package com.atxwq.shop.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.atxwq.shop.model.dto.product.ProductSkuDto;
import com.atxwq.shop.model.entity.product.Product;
import com.atxwq.shop.model.entity.product.ProductDetails;
import com.atxwq.shop.model.entity.product.ProductSku;
import com.atxwq.shop.model.vo.h5.ProductItemVo;
import com.atxwq.shop.product.mapper.ProductDetailsMapper;
import com.atxwq.shop.product.mapper.ProductMapper;
import com.atxwq.shop.product.mapper.ProductSkuMapper;
import com.atxwq.shop.product.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2025/5/18 13:46
 * @Created by 12336
 */

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List<ProductSku> selectSkuProductList() {
        List<ProductSku> productSkuList = productSkuMapper.selectSkuProductList();
        return productSkuList;
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }

    /**
     * 商品详情
     * @param skuId
     * @return
     */
    @Override
    public ProductItemVo item(Long skuId) {
        //1 获取当前sku信息
        ProductSku productSku = productSkuMapper.getById(skuId);
        //2 根据skuId获取productId,获取商品信息
        Long productId = productSku.getProductId();
        //3 同一个商品下所有sku信息
        List<ProductSku> productSkuList = productSkuMapper.selectSkuProductList();
        ProductDetails productDetails = productDetailsMapper.selectByProductId(productId);
        //建立sku规格以及skuId所对应的关系
        Map<String,Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });
        //4 获取商品详细信息 根据商品skuId
        Product product = productMapper.getProduct(productId);

        ProductItemVo productItemVo = new ProductItemVo();
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);
        return productItemVo;
    }

    /**
     * 根据skuId获取sku信息
     * @param skuId
     * @return
     */
    @Override
    public ProductSku getBySkuId(Long skuId) {
        ProductSku productSku = productSkuMapper.getById(skuId);
        return productSku;
    }
}
